package cn.hotel.servlet;

import cn.hotel.pojo.RoomKeeper;
import cn.hotel.service.RoomKeeperService;
import cn.hotel.service.RoomKeeperServiceImpl;
import cn.hotel.utlis.Paging;
import cn.hotel.utlis.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/keeper")
public class RoomKeeperServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();

        RoomKeeperService roomKeeperService = new RoomKeeperServiceImpl();
        String type = request.getParameter("type");
        type = type == null? "inquire" : type;
        switch (type){
            //分页查询
            case "inquire":
                inquireRoomKeeper(request,response,roomKeeperService);
                break;
            //添加
            case "add":
                addRoomKeeper(request,response,roomKeeperService,out);
                break;
            //删除
            case "delete":
                deleteRoomKeeper(request,response,roomKeeperService,out);
                break;
        }
    }

    //删除
    private void deleteRoomKeeper(HttpServletRequest request, HttpServletResponse response, RoomKeeperService roomKeeperService, PrintWriter out) {
        String appPath = request.getContextPath();
        String rooms = request.getParameter("rooms");
        Map<String ,Object> objectMap = roomKeeperService.deleteRoomKeeper(StringUtil.strToInt(rooms));
        if("1".equals(objectMap.get("flag"))){
            out.print("<script>alert('删除成功');location.href='"+appPath+"/keeper?type=inquire'</script>");
        }else {
            out.print("<script>alert('删除失败');location.href='"+appPath+"/keeper?type=inquire'</script>");
        }
        out.flush();
    }

    //添加
    private void addRoomKeeper(HttpServletRequest request, HttpServletResponse response, RoomKeeperService roomKeeperService, PrintWriter out) {
        String appPath = request.getContextPath();
        String roomName = request.getParameter("roomName");
        String typeName = request.getParameter("typeName");
        Map<String ,Object> objectMap = roomKeeperService.numType(StringUtil.strToInt(roomName));
        if("1".equals(objectMap.get("flag"))){
            out.print("<script>alert('房间编号重复');location.href='"+appPath+"/keeper?type=inquire'</script>");
            out.flush();
            return;
        }
        if(StringUtil.strToInt(typeName) == 0){
            out.print("<script>alert('请选择房型');location.href='"+appPath+"/keeper?type=inquire'</script>");
            out.flush();
        }else {
            cn.hotel.pojo.RoomKeeper roomKeeper = new RoomKeeper();
            roomKeeper.setRoomNumber(StringUtil.strToInt(roomName));
            roomKeeper.setHouseTypeId(StringUtil.strToInt(typeName));
            Map<String ,Object> stringObjectMap = roomKeeperService.addRoomKeeper(roomKeeper);
            if("1".equals(stringObjectMap.get("flag"))){
                out.print("<script>alert('添加成功');location.href='"+appPath+"/keeper?type=inquire'</script>");
                out.flush();
            }else {
                out.print("<script>alert('添加失败');location.href='"+appPath+"/keeper?type=inquire'</script>");
                out.flush();
            }
        }
    }

    //分页查询
    private void inquireRoomKeeper(HttpServletRequest request, HttpServletResponse response, RoomKeeperService roomKeeperService) throws ServletException, IOException {
        Paging<cn.hotel.pojo.RoomKeeper> keeperPaging = new Paging<>();
        //获取房间号
        String room = request.getParameter("names");
        //获取当前页
        int num = StringUtil.strToIntFirst(request.getParameter("current"));

        //获取总条数
        Map<String ,Object> atPresentMap = roomKeeperService.roomKeeper(StringUtil.strToInt(room));
        if("1".equals(atPresentMap.get("flag"))){
            keeperPaging.setSumData((Integer) atPresentMap.get("data"));
        }
        num = num > keeperPaging.getSumPage()? keeperPaging.getSumPage() : num;
        keeperPaging.setCurrent(num);
        //获取数据
        Map<String,Object> objectMap = roomKeeperService.som(StringUtil.strToInt(room), keeperPaging.getCurrent(), keeperPaging.getNumData());
        if("1".equals(objectMap.get("flag"))){
            keeperPaging.setPagingList((List<RoomKeeper>) objectMap.get("data"));
        }
        request.setAttribute("room",room);
        request.setAttribute("keeperPaging",keeperPaging);
        request.getRequestDispatcher("logjsp/roomkeeper.jsp").forward(request,response);
    }
}
